package com.randi.common.utils.page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.randi.common.core.domain.BaseParam;

import java.util.Collections;

/**
 * @author ziye
 */
public class PageHelperUtils {

    private static final Integer PAGE_NUM = 1;

    private static final Integer PAGE_SIZE = 20;

    /**
     * 初始化分页参数
     *
     * @param baseParam BaseParam
     */
    public static void initPage(BaseParam baseParam) {
        if (baseParam == null) {
            baseParam = new BaseParam();
        }
        if (baseParam.getPageNum() == null) {
            baseParam.setPageNum(PAGE_NUM);
        }
        if (baseParam.getPageSize() == null) {
            baseParam.setPageSize(PAGE_SIZE);
        }
        // 设置分页参数
        PageHelper.startPage(baseParam.getPageNum(), baseParam.getPageSize());
    }

    /**
     * 初始化分页参数
     *
     * @return PageInfo
     */
    public static <T> PageInfo<T> initEmptyList() {
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(PAGE_NUM);
        pageInfo.setPageSize(PAGE_SIZE);
        pageInfo.setTotal(0L);
        pageInfo.setPages(0);
        pageInfo.setList(Collections.emptyList());
        return pageInfo;
    }
}
